/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;


import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class CreateServiceBusBindingContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;
	
	private VerticalPanel panel = null;
	
	private ListBox bus;
	
	private CheckBox published;

	
//	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
//		public void onSuccess(ResponseObject res) {
//			String code = res.getCode();
//			if (!code.equals("0")) {
//				Window.alert("There was an error: " + res.getMsg());
//			}
//			ArrayList ret = (ArrayList)res.getResponse();
//			//bus.setVisibleItemCount(10);
//			bus.addItem("", "");
//			String bid = (String)context.get(ServiceConstants.BUS_ID);
//			for(int i = 0, j = ret.size(); i < j ; i ++){
//				KeyNamePairs k = (KeyNamePairs)ret.get(i);
//				bus.addItem(k.getName(),k.getId());
//				if(k.getId().equals(bid)){
//					bus.setSelectedIndex(i);
//				}
//			}
//
//			
//		}
//
//		public void onFailure(Throwable caught) {
//			Window.alert("There was an error: " + caught.toString());
//		}
//	};
	
	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			//Service s = (Service)context.get(ServiceConstants.SERVICE);
			//s.setId((String)res.getResponse());
			context.remove(ServiceConstants.SERVICE);
			context.remove(ServiceConstants.SERVICE_QOS);
			context.remove(ServiceConstants.BUS_ID);
			HashMap p = new HashMap();
			p.put("id", (String)res.getResponse());
			listener.onContentChanged(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING,
					ServiceContentConstants.SERVICE_ROOT,p );
			

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	
	public CreateServiceBusBindingContent(ServiceMessages cons,BifangGWTServiceAsync s,GwtContext c){
		this.service = s;
		this.cons = cons;
		this.context = c;
		
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");
		
		initWidget(panel);
		
	}
	
	public void onLoad(){
		HTML h = new HTML(cons.bindingBus());
		panel.add(h);
		bus = new ListBox();
		bus.setWidth("11em");
		
		List ret = (List)context.get(ServiceConstants.BUS_LIST);
		//bus.setVisibleItemCount(10);
		bus.addItem("", "");
		String bid = (String)context.get(ServiceConstants.BUS_ID);
		for(int i = 0, j = ret.size(); i < j ; i ++){
			KeyNamePairs k = (KeyNamePairs)ret.get(i);
			bus.addItem(k.getName(),k.getId());
			if(k.getId().equals(bid)){
				bus.setSelectedIndex(i);
			}
		}
		
		panel.add(bus);
		panel.add(new HTML("&nbsp;"));
		published = new CheckBox(cons.publishService());
		panel.add(published);
		
		panel.add(new HTML("&nbsp;"));
		Button b = new Button(cons.previous());
		b.addClickListener(new ClickListener(){
			public void onClick(Widget send){
				if(bus.getSelectedIndex() > 0){
					context.put(ServiceConstants.BUS_ID, bus.getValue(bus.getSelectedIndex()));
				}
				listener.onContentChanged(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING,
						ServiceContentConstants.CREATE_SERVICE_CONFIG, null);
			}
			
		});
		
		Button b1 = new Button(cons.create());
		b1.addClickListener(new ClickListener(){
			public void onClick(Widget send){
				String busId = null;
				if(bus.getSelectedIndex() > 0 ){
					busId = bus.getValue(bus.getSelectedIndex());
				}
				ArrayList s = new ArrayList();
				Service ser = (Service)context.get(ServiceConstants.SERVICE);
				if(published.isChecked()){
					ser.setStatus(EntityConstants.SERVICE_STATUS_PUBLISHED);
				}else{
					ser.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
				}
				
				s.add(ser);
				s.add(context.get(ServiceConstants.SERVICE_QOS));
				s.add(busId);
				RequestObject req = new RequestObject();
				req.setAction("han.bifang.gwt.server.action.service.CreateServiceAction");
				req.setRequest(s);
				service.call(req, cb1);
			}
			
		});
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(b);
		hp.add(new HTML("&nbsp;"));
		hp.add(b1);
		
		panel.add(hp);
//		RequestObject req = new RequestObject();
//		req.setAction("han.bifang.gwt.server.action.service.GetDomainBusListAction");
//		//String id = (String)context.get(ServiceConstants.APP_ID);
//		//req.setRequest(id);
//		
//		service.call(req, cb);
		
		
		
	}

}
